package com.gpframework.module.controller.wages;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.exception.BaseException;
import com.gpframework.module.service.wages.IGzGenerateMonthlyPayrollService;
import com.gpframework.module.wages.domain.GzPersonnelFoundation;
import com.gpframework.module.wages.domain.req.GzGenerateReq;
import com.gpframework.module.wages.domain.req.GzOfficeSalaryReq;
import com.gpframework.module.wages.domain.req.GzTransferReq;
import com.gpframework.module.wages.domain.req.GzWarehousePersonnelReq;
import com.gpframework.module.wages.domain.vo.SalarySummaryVo;
import com.gpframework.module.wages.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Month;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Validated
@Api(tags = "生成月度工资单")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/wages/Generatemonthlypayroll" )
public class GzGenerateMonthlyPayrollController {

    //人员基础
    private final IGzPersonnelFoundationService iGzPersonnelFoundationService;
    private final IGzGenerateMonthlyPayrollService iGzGenerateMonthlyPayrollService;

    @ApiOperation("生成档口及办公室月度工资单")
    @PostMapping("/generateOfficeSalary")
    public AjaxResult generateOffice(@RequestBody GzOfficeSalaryReq gzOfficeSalaryReq)  throws BaseException {
        List<GzPersonnelFoundation> gzPersonnelFoundations = iGzPersonnelFoundationService.lambdaQuery().list();
        //生成档口及办公室工资条
        List<GzPersonnelFoundation> office = new ArrayList<>();
        for (GzPersonnelFoundation gzPersonnelFoundation : gzPersonnelFoundations) {
            //生成档口及办公室工资条
            if (gzPersonnelFoundation.getDepartmentType() == 1) {
                office.add(gzPersonnelFoundation);
            }

        }
        //生成档口及办公室工资条
        if (CollUtil.isNotEmpty(office)) {
            iGzGenerateMonthlyPayrollService.generateOfficeSalary(gzOfficeSalaryReq.getMonth(), office);
        }
        return AjaxResult.success();

    }

    @ApiOperation("生成中转人员工资工资单")
    @PostMapping("/generateGzTransfer")
    public AjaxResult generateGzTransfer(@RequestBody GzTransferReq gzTransferReq) {
        List<GzPersonnelFoundation> gzPersonnelFoundations = iGzPersonnelFoundationService.lambdaQuery().list();
        //生成中转
        List<GzPersonnelFoundation> gzTransfer = new ArrayList<>();

        for (GzPersonnelFoundation gzPersonnelFoundation : gzPersonnelFoundations) {

            if (gzPersonnelFoundation.getDepartmentType() == 2) {
                gzTransfer.add(gzPersonnelFoundation);
            }
        }

        //生成中转工资条
        if (CollUtil.isNotEmpty(gzTransfer)) {
            iGzGenerateMonthlyPayrollService.generateTransferPayroll(gzTransferReq.getMonth(), gzTransfer);
        }
        return AjaxResult.success();
    }

    @ApiOperation("生成仓库工资条")
    @PostMapping("/generateGzWarehousePersonnel")
    public AjaxResult generateGzWarehousePersonnel(@RequestBody GzWarehousePersonnelReq gzWarehousePersonnelReq) {
        List<GzPersonnelFoundation> gzPersonnelFoundations = iGzPersonnelFoundationService.lambdaQuery()
                .list();
        //生成仓库
        List<GzPersonnelFoundation> gzWarehousePersonnel = new ArrayList<>();

        for (GzPersonnelFoundation gzPersonnelFoundation : gzPersonnelFoundations) {
            //生成仓库
            if (gzPersonnelFoundation.getDepartmentType() == 3) {
                gzWarehousePersonnel.add(gzPersonnelFoundation);
            }

            //生成仓库工资条
            if (CollUtil.isNotEmpty(gzWarehousePersonnel)) {
                iGzGenerateMonthlyPayrollService.generateWarehousePayroll(gzWarehousePersonnelReq.getMonth(), gzWarehousePersonnel);
            }

        }
        return AjaxResult.success();
    }


}
